package com.kuang.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.Date;

public class JsonUtils {

    public static String getJson(Object obj,String sdf){
        ObjectMapper mapper = new ObjectMapper();

        //不使用时间搓的方式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

        //自定义日期的格式
        SimpleDateFormat format = new SimpleDateFormat(sdf);

        //自己设置时间格式
        mapper.setDateFormat(format);

        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getJson(Object obj){
        return getJson(obj,"yyyy:MM:dd HH:mm:ss");
    }
}
